married(X, Y) :- marriage(Y, X); marriage(X, Y).
husband(X, Y) :- male(X), married(Y, X).
wife(X, Y) :- female(X), married(Y, X).
father(X, Y) :- male(X), parent(X, Y).
mother(X, Y) :- female(X), parent(X, Y).
child(X, Y) :- parent(Y, X).
son(X, Y) :- male(X), child(Y, X).
daughter(X, Y) :- female(X), child(Y, X).
sibling(X, Y) :- parent(P, X), parent(P, Y).
brother(X, Y) :- male(X), sibling(X, Y).
sister(X, Y) :- female(X), sibling(X, Y).
grandfather(X,Y) :- male(X),parent(X,Z),parent(Z,Y).
grandmother(X,Y) :- female(X),parent(X,Z),parent(Z,Y).
uncle(X,Y):-male(X),parent(Z,Y),sibling(Z,X), Z\=X,Y\=X.
aunt(X,Y):-female(X),parent(Z,Y),sibling(Z,X), Z\=X,Y\=X.

ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
descendant(X, Y) :- ancestor(Y, X).

inFatherLine(X, Y) :- father(X, Y).
inFatherLine(X, Y) :- father(X, Z), inFatherLine(Z, Y).

inMotherLine(X, Y) :- mother(X, Y).
inMotherLine(X, Y) :- mother(X, Z), inMotherLine(Z, Y).

ancestorN(X, Y, 1) :- parent(X, Y).
ancestorN(X, Y, N) :- parent(X, Z), ancestorN(Z, Y, NN), N is NN+1.

descendantN(X,G,Y):- ancestorN(Y, X, G).

rightfulChild(X, Y) :- child(X,Y), married(Y,Z), child(X,Z).
bastard(X,Y) :- rightfulChild(X,Y), !, fail; child(X,Y).

count([], 0).
count([_|Els], Count) :-
	count(Els, C),
	Count is (C + 1).
	

numberOfChildren(P,Count) :-
    findall(1,parent(P,_),L),
    count(L,Count).

numberOfDescendants(P,Count) :-
    findall(1,ancestor(P,_),L),
    count(L,Count).
	
